import { SequelizeModel, AdminModel, UserModel } from '@model'
import { success, error } from '@res'
import moment from 'moment'

const Sequelize = require('sequelize')
const Op = Sequelize.Op

class Admin {

  // 登录
  static async login (ctx) {
    const params = ctx.request.body
    const result = await AdminModel.findOne({
      where: {
        account: params.account,
        password: params.password
      }
    })
    if (!result) {
      ctx.body = error('账号或密码错误')
      return
    }
    ctx.body = success(result)
  }

  // 用户列表
  static async getUserList (ctx) {
    const params = ctx.request.body
    const result = await UserModel.findAndCountAll({
      limit: Number(params.limit),
      offset: (Number(params.pageNo) - 1) * Number(params.limit),
      order: [
        ['id', 'DESC'],
      ]
    })
    ctx.body = success(result)
  }
}

export default Admin